// pages/seat_choose/seat_choose.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value1: '',
        value2: '',
        cg_name: '',
        lc_name: '',
        start_time: '',
        end_time: '',
        seat_list: '',
        choose: '',
        selectedNum: 0,
        index: '',
        user: '',
        id: '',
        listData: [],
        zcyy_id: ''
    },
    //我选的座位
    mychoose(e) {
        let index = e.currentTarget.dataset.index
        this.setData({ index: index })
        this.setData({ id: e.currentTarget.id })
        this.setData({ choose: (index + 1) + '号座位' })
        let seat_list = this.data.seat_list
        let seat = []
        if (seat_list[index].seat_state == 1) {
            this.setData({ choose: '--' })
        } else if (seat_list[index].seat_state == 3) {
            this.setData({ choose: '--' })
        } else if (seat_list[index].seat_state == 0) {
            seat[seat.length] = index
            seat_list[index].seat_state = 2
            let selectedNum = this.data.selectedNum + 1
            this.setData({
                seat_list: seat_list,
                selectedNum,
            })
            if (this.data.selectedNum > 0) {
                seat_list[index].seat_state = 0
            }
        }
    },
    //预约
    Appointment() {
        db.collection("user").where({ phone: this.data.user }).get().then(res => {
            var user_seat = res.data[0].user_seat
            if (user_seat == '1') {
                wx.showToast({
                    title: '你已经预约过啦',
                    icon: 'error'
                })
            } else {
                let index = this.data.index
                let seat_list = this.data.seat_list
                if (seat_list[index].seat_state == 1) {
                    wx.showToast({
                        title: '已预约',
                        icon: 'none'
                    })
                } else {
                    if (seat_list[index].seat_state == 3) {
                        wx.showToast({
                            title: '不可预约',
                            icon: 'none'
                        })
                    } else {
                        db.collection("userInfo").add({
                            data: {
                                phone: this.data.user,
                                seat_id: this.data.index,
                                seat_state: 0,
                                _createTime: Date.parse(new Date()),
                                id: this.data.id,
                                cg_name: this.data.cg_name,
                                start_time: this.data.start_time,
                                end_time: this.data.end_time,
                                cg_id: this.data.value2
                            }
                        }).then(res => {
                            wx.showToast({
                                title: '选座成功',
                                icon: 'success',
                                duration: 2000,
                                success: () => {
                                    db.collection("time").doc(this.data.value2).update({
                                        data: {
                                            ['seat_list.' + this.data.index + '.seat_state']: 1
                                        },
                                        success: res => {
                                            db.collection("user").doc(this.data.user).update({
                                                data: {
                                                    user_seat: 1
                                                },
                                                success: res => {
                                                    setTimeout(function () {
                                                        wx.switchTab({
                                                            url: '../index/index',
                                                        })
                                                    }, 2000)
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    }
                }
            }
        })
    },
    //获取场馆信息
    getVenue_info(){
        db.collection("Seat_Reservation").doc(this.data.value1).get().then(res => {
            this.setData({
                cg_name: res.data.dating_name,
                lc_name: res.data.louceng_name,
            })
        })
    },
    //获取时间信息
    gettime_info(){
        db.collection("time").doc(this.data.value2).get().then(res => {
            this.setData({
                seat_list: res.data.seat_list,
                start_time: res.data.start_time,
                end_time: res.data.end_time
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            value1: options.value1,
            value2: options.value2,
        })
        this.gettime_info()
        this.getVenue_info()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },



    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let user = wx.getStorageSync('user')
        this.setData({
            user: user._id
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})